﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using System.Net;

namespace LocalClient
{
    public class MessageInitializer
    {
        private const int bufferSize = 512;

        public void StartWorking()
        {
            TcpClient client = ConnectToServer();

            NetworkStream clientStream = client.GetStream();
            /*
             * I create message and send his to server via |clientStream| Element
             */
            string someMyMsg = "some Interesting my bad message";
            byte[] requestBuffer = Encoding.ASCII.GetBytes(someMyMsg);
            clientStream.Write(requestBuffer, 0, requestBuffer.Length);

            clientStream.Close();
            client.Close();
        }

        private TcpClient ConnectToServer()
        {
            // Connect to the server.
            TcpClient client = new TcpClient();

            IPHostEntry host = Dns.GetHostEntry(
               Properties.Settings.Default.Server);
            var address = (from h in host.AddressList
                           where h.AddressFamily == AddressFamily.InterNetwork
                           select h).First();
            client.Connect(address.ToString(),
               Properties.Settings.Default.ServerPort);

            return client;
        }
    }
}
